package org.deri.feather.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTools {
	
	public static boolean isURI(String candidateURI){
		  
		if(candidateURI.contains("http://") && !candidateURI.contains("^^"))
			return true;
		  
		return false;
	}
	
	public static String cleanURI(String inputString){
		
		if (isURI( inputString))
			return inputString.replace("<", "").replace(">", "").trim();
		else
			return  inputString;
	}

	public static String uriToString(String inputString){
	  	
		String outputString = "";
			
		if(!inputString.contains("\"^^")){
		
			inputString = inputString.replace(">", "").replace("<", "").trim();
		  	String inputStringNoURI = inputString.substring(inputString.lastIndexOf("/")+1, inputString.length());
		  	
		  	if(inputStringNoURI.contains("#"))
		  		inputStringNoURI = inputString.substring(inputString.lastIndexOf("#")+1, inputString.length());
		  	
		  	if(inputStringNoURI.contains("_"))
		  		inputStringNoURI = inputStringNoURI.replaceAll("_", " ");
	
		  	if(inputStringNoURI.contains(":"))
		  		inputStringNoURI = inputStringNoURI.replaceAll(":", "");
		  	
		  	if(inputStringNoURI.contains("\""))
		  		inputStringNoURI = inputStringNoURI.replaceAll("\"", "");
		  			
		  	if(inputStringNoURI.contains("@"))
		  		inputStringNoURI = inputStringNoURI.replaceAll(inputStringNoURI.substring(inputStringNoURI.indexOf("@"), inputStringNoURI.indexOf("@")+3),"");
		  	
		      Pattern p = Pattern.compile("\\p{Lu}");
		      Matcher m = p.matcher(inputStringNoURI);
		      StringBuffer sb = new StringBuffer();
		      while (m.find()) {
		          m.appendReplacement(sb, " " + m.group());
		      }
		      m.appendTail(sb);
		      outputString = sb.toString().trim();
		
		}else{
			if(inputString.contains("\"^^"))
				outputString = inputString.substring(0, inputString.indexOf("\"^^")).trim();
		}
			
		return outputString;
	 }

}
